<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/19 20:26
 */

namespace module\chat\front;

use module\ai\model\Channel;
use mof\annotation\Description;
use mof\Model;
use mof\utils\DictArray;

class Config extends \mof\front\Config
{
    #[Description('基础设置')]
    public function groupBase(array $values): array
    {
        return [
            [
                "label"                 => "选择AI通道",
                "prop"                  => "channel_ids",
                "type"                  => "select-multiple",
                "value"                 => array_map('intval', $values['channel_ids'] ?? []),
                "collapse-tags"         => true,
                "collapse-tags-tooltip" => true,
                "options"               => DictArray::make($this->getChannelList())
                    ->toElementData()->toSelectOptions(),
                "intro"                 => "选择对话时允许用户使用的AI通道。"
            ],
            [
                "label" => "携带历史对话",
                "prop"  => "history_chat_freq",
                "type"  => "input-number",
                "value" => $values['history_chat_freq'] ?? 1,
                "intro" => "用户在与AI对话时携带的记录条数（问和答算1轮历史对话），携带越多，聊天记录越完整，" .
                    "但消耗的token越多。留空或0表示不携带历史记录。"
            ]
        ];
    }

    #[Description('自由对话')]
    public function groupChat(array $values): array
    {
        return [
            [
                "label"  => "角色头像",
                "type"   => "upload:image",
                "prop"   => "avatar",
                "value"  => $values['avatar'] ?? '',
                "intro"  => "用户与AI对话时的头像",
                "_extra" => ['client_cfg' => 1],
            ],
            [
                "label" => "角色描述",
                "prop"  => "role_intro",
                "type"  => "textarea",
                "value" => $values['role_intro'] ?? '你是由磨锋软件开发工作室提供的AI助手。',
                "intro" => "用户与AI对话时，对AI角色定位的描述。",
            ],
            [
                "label"  => "开场白",
                "prop"   => "welcome_text",
                "type"   => "textarea",
                "value"  => $values['welcome_text'] ?? '您有什么问题需要解决，请描述。',
                "intro"  => "首次对话时，页面上显示的欢迎语。",
                "_extra" => ['client_cfg' => 1],
            ],
            [
                "label"    => "示例问题",
                "prop"     => "example_questions",
                "type"     => "textarea",
                "value"    => $values['example_questions'] ?? '',
                "intro"    => "用户首次对话时，对话框会显示此示例问题。一行一个问题。",
                "_extra"   => ['client_cfg' => 1],
            ],
            [
                "label"  => "输入提示",
                "prop"   => "prompt_placeholder",
                "type"   => "input",
                "value"  => $values['prompt_placeholder'] ?? '请输入问题...',
                "intro"  => "用户在对话页的输入框中看到的提示信息。",
                "_extra" => ['client_cfg' => 1],
            ],
        ];
    }

    #[Description('界面配置')]
    public function groupPage(array $values): array
    {
        $sloganTypes = [
            'unlimited' => '自由对话',
            'assistant' => '创作助手',
            'visrole'   => '虚拟角色'
        ];

        $slogans = array_map(function ($value, $label) use ($values) {
            $prop = "slogans.{$value}";
            return [
                "label"    => "{$label}广告语",
                "prop"     => $prop,
                "value"    => $values['slogans'][$value] ?? '',
                "type"     => "textarea",
                "intro"    => "{$label}的广告语，一行一条",
                "_visible" => "show_slogan=in:{$value}",
                "_extra"   => ['client_cfg' => 1],
            ];
        }, array_keys($sloganTypes), array_values($sloganTypes));

        return [
            [
                "label"   => "显示广告语",
                "prop"    => "show_slogan",
                "type"    => "checkbox",
                "value"   => $values['show_slogan'] ?? [],
                "options" => array_map(
                    fn($value, $label) => ["caption" => $label, "value" => $value],
                    array_keys($sloganTypes), array_values($sloganTypes)
                ),
                "intro"   => "在前端相关页面显示广告语",
                "_extra"  => ['client_cfg' => 1],
            ],
            ...$slogans
        ];
    }

    /**
     * 给用户模块提供消费信息
     * @return array
     */
    public function consumption(): array
    {
        $result = [];
        $channels = $this->getChannelList();
        foreach ($channels as $id => $name) {
            $result[] = [
                'name'  => $id,
                'label' => $name,
            ];
        }
        return $result;
    }

    private function getChannelList(): array
    {
        return Channel::where('status', 1)->column('name', 'id');
    }
}